package com.reggie.setmeal.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.reggie.setmeal.domain.Setmeal;
import com.reggie.setmeal.uuid.SnowflakeIdUtils;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.reggie.setmeal.mapper.SetmealDishMapper;
import com.reggie.setmeal.domain.SetmealDish;
import com.reggie.setmeal.service.ISetmealDishService;

/**
 * 套餐菜品关系Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-05-02
 */
@Service
public class SetmealDishServiceImpl implements ISetmealDishService 
{
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 查询套餐菜品关系
     * 
     * @param id 套餐菜品关系主键
     * @return 套餐菜品关系
     */
    @Override
    public SetmealDish selectSetmealDishById(Long id)
    {
        return setmealDishMapper.selectSetmealDishById(id);
    }

    /**
     * 查询套餐菜品关系列表
     * 
     * @param setmealDish 套餐菜品关系
     * @return 套餐菜品关系
     */
    @Override
    public List<SetmealDish> selectSetmealDishList(SetmealDish setmealDish)
    {
        return setmealDishMapper.selectSetmealDishList(setmealDish);
    }

    /**
     * 新增套餐菜品关系
     * 
     * @param setmealDish 套餐菜品关系
     * @return 结果
     */
    @Override
    public int insertSetmealDish(SetmealDish setmealDish)
    {
        setmealDish.setCreateTime(DateUtils.getNowDate());
        setmealDish.setUpdateTime(DateUtils.getNowDate());
        setmealDish.setCreateUser(SecurityUtils.getUserId());
        setmealDish.setUpdateUser(SecurityUtils.getUserId());
        return setmealDishMapper.insertSetmealDish(setmealDish);
    }

    /**
     * 修改套餐菜品关系
     * 
     * @param setmealDish 套餐菜品关系
     * @return 结果
     */
    @Override
    public int updateSetmealDish(SetmealDish setmealDish)
    {
        setmealDish.setUpdateTime(DateUtils.getNowDate());
        return setmealDishMapper.updateSetmealDish(setmealDish);
    }

    /**
     * 批量删除套餐菜品关系
     * 
     * @param ids 需要删除的套餐菜品关系主键
     * @return 结果
     */
    @Override
    public int deleteSetmealDishByIds(Long[] ids)
    {
        return setmealDishMapper.deleteSetmealDishByIds(ids);
    }

    /**
     * 删除套餐菜品关系信息
     * 
     * @param id 套餐菜品关系主键
     * @return 结果
     */
    @Override
    public int deleteSetmealDishById(Long id)
    {
        return setmealDishMapper.deleteSetmealDishById(id);
    }

    @Override
    public List<SetmealDish> selectSetmealDishBySetMealId(Long id) {
        return setmealDishMapper.selectSetmealDishBySetMealId(id);
    }

    @Override
    public int updateSetmealDishBySetmealId(SetmealDish setmealDish) {
        setmealDish.setCreateTime(DateUtils.getNowDate());
        setmealDish.setUpdateTime(DateUtils.getNowDate());
        setmealDish.setCreateUser(SecurityUtils.getUserId());
        setmealDish.setUpdateUser(SecurityUtils.getUserId());
        return setmealDishMapper.updateSetmealDishBySetmealId(setmealDish);
    }

    @Override
    public int updateSetmealDishBySetmeal(Setmeal setmeal) {
        int i = 0;
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        setmealDishMapper.deleteSetmealDishBySetmealId(setmeal.getId());
        if (null==setmealDishes||setmealDishes.size()==0){
            return 1;
        }

        //添加
        for (SetmealDish setmealDish : setmealDishes) {
            
            if (setmealDish.getId().equals(setmealDish.getDishId())) {
                setmeal.setId(new SnowflakeIdUtils(5, 8).nextId());
            }
            setmealDish.setSetmealId(setmeal.getId() +"");
            setmealDish.setCreateTime(DateUtils.getNowDate());
            setmealDish.setUpdateTime(DateUtils.getNowDate());
            setmealDish.setCreateUser(SecurityUtils.getUserId());
            setmealDish.setUpdateUser(SecurityUtils.getUserId());
            i += setmealDishMapper.insertSetmealDish(setmealDish);
        }
        return i;
    }
}
